import rx.Observable;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by codeczx on 2017/2/22.
 */
public class CombiningSequencesTest {

    void testSwitchOnNext() {
        Observable.switchOnNext(
                Observable.interval(100, TimeUnit.MILLISECONDS)
                        .map(i ->
                                Observable.interval(30, TimeUnit.MILLISECONDS)
                                        .map(i2 -> i)
                        )
        )
                .take(9)
                .subscribe(new PrintSubscriber("switchOnNext"));
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void testSwitchMap() {
        Observable.interval(100, TimeUnit.MILLISECONDS)
                .switchMap(i->Observable.interval(30,TimeUnit.MILLISECONDS)
                .map(l->i))
                .take(5)
                .subscribe(System.out::println);
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void testCombineLatest() {
        Observable.combineLatest(
                Observable.interval(100, TimeUnit.MILLISECONDS),
                Observable.interval(150, TimeUnit.MILLISECONDS),
                Observable.interval(200, TimeUnit.MILLISECONDS),
                (i1, i2, i3) -> i1 + "-" + i2 + "-" + i3)
                .take(5)
                .subscribe(System.out::println);
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        CombiningSequencesTest cst = new CombiningSequencesTest();
//        cst.testSwitchOnNext();
//        cst.testCombineLatest();
        cst.testSwitchMap();
    }
}
